package com.yunhe.common.util;

import com.yunhe.common.client.WebAuthorityClient;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.model.authority.Station;
import com.yunhe.common.model.authority.StationDTO;
import com.yunhe.common.model.authority.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求头工具类
 * @author liuronglei
 */

@Component
public class HeaderUtil {
    @Autowired
    private WebAuthorityClient webAuthorityClient;

    public User getHeaderUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userIdStr = request.getHeader(SystemConsts.HEADER.USER_ID.value());
        if (StringUtil.isEmpty(userIdStr)) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        return webAuthorityClient.getUserById(userId).getBody().getResults();
    }

    public Long getHeaderUserId() {
        ServletRequestAttributes attributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
        if (attributes == null) {
            return null;
        }
        String userIdStr = attributes.getRequest().getHeader(SystemConsts.HEADER.USER_ID.value());
        if (attributes != null) {

        }
        if (userIdStr == null || userIdStr.length() == 0) {
            return null;
        }
        Long userId = Long.parseLong(userIdStr);
        return userId;
    }


    public Station getHeaderStation() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String stationIdStr = request.getHeader(SystemConsts.HEADER.STATION_ID.value());
        if (StringUtil.isEmpty(stationIdStr)) {
            return null;
        }
        Long stationId = Long.parseLong(stationIdStr);
        return webAuthorityClient.getStationById(stationId).getBody().getResults();
    }

    public Long getHeaderStationId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String stationIdStr = request.getHeader(SystemConsts.HEADER.STATION_ID.value());
        if (stationIdStr == null || stationIdStr.length() == 0) {
            return null;
        }
        Long stationId = Long.parseLong(stationIdStr);
        return stationId;
    }

//    public FirmDTO getHeaderFirm() {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String firmIdStr = request.getHeader(SystemConsts.HEADER.FIRM_ID.value());
//        if (StringUtils.isEmpty(firmIdStr)) {
//            return null;
//        }
//        Long firmId = Long.parseLong(firmIdStr);
//        return webAuthorityClient.getFirmById(firmId).getBody().getResults();
//    }

    public Long getHeaderFirmId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String firmIdStr = request.getHeader(SystemConsts.HEADER.FIRM_ID.value());
        if (firmIdStr == null || firmIdStr.length() == 0) {
            return null;
        }
        Long firmId = Long.parseLong(firmIdStr);
        return firmId;
    }


    public String getHeader(String name) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getHeader(name);
    }
}
